import 'package:city_pickers/city_pickers.dart';
import 'package:city_pickers/modal/result.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:yp/helper.dart';
import 'package:yp/pages/address_update/widgets/text_input.dart';
import 'package:yp/widgets/button_widget.dart';
import 'package:yp/widgets/select_button.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:kzcity_picker/kzcity_picker.dart';
import 'logic.dart';
import 'state.dart';
import 'package:yp/pages/address_selector/cy_city_picker.dart';

class AddressUpdatePage extends StatelessWidget {
  final AddressUpdateLogic logic = Get.put(AddressUpdateLogic());
  final AddressUpdateState state = Get.find<AddressUpdateLogic>().state;

  @override
  Widget build(BuildContext context) {
    state.addressId = Get.arguments["adr_num_id"].toString();
    print(">>>>>>>>>>>${Get.arguments}");
    return Scaffold(
        resizeToAvoidBottomInset: false,
        appBar: AppBar(
          backgroundColor: Config.mainColor,
          brightness: Brightness.light,
          iconTheme: IconThemeData(color: Colors.white),
          title: Text(
            "修改地址",
            style: TextStyle(color: Colors.white, fontSize: 32.sp),
          ),
          centerTitle: true,
          elevation: 0,
        ),
        body: GetBuilder<AddressUpdateLogic>(builder: (logic) {
          return Container(
              height: double.infinity,
              child: SingleChildScrollView(
                  child: Column(
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      // 收货人
                      TextInputWidget(
                        title: "收货人",
                        hintText: "请输入收货人名字",
                        controller: state.controller1,
                      ),
                      // 性别
                      // TextInputWidget(
                      //   title: "性别",
                      //   suffixWidget: GetBuilder<AddressUpdateLogic>(
                      //     builder: (logic) {
                      //       return Row(
                      //         mainAxisAlignment: MainAxisAlignment.start,
                      //         children: [
                      //           SelectButton(
                      //             title: "先生",
                      //             isSelected: logic.state.sex == 1,
                      //             onTap: () => logic.setSex(1),
                      //           ),
                      //           SizedBox(width: 20.w),
                      //           SelectButton(
                      //             title: "女士",
                      //             isSelected: logic.state.sex == 2,
                      //             onTap: () => logic.setSex(2),
                      //           ),
                      //         ],
                      //       );
                      //     },
                      //   ),
                      // ),
                      // 手机号
                      TextInputWidget(
                        title: "手机号",
                        hintText: "请输入手机号",
                        controller: state.controller2,
                      ),
                      // 所在地区
                      TextInputWidget(
                        title: "所在地区",
                        suffixWidget: GestureDetector(
                          onTap: () {
                            _clickEventFunc(context);
                          },
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              // Text(
                              //   state.controller3.text,
                              //   style: TextStyle(
                              //     fontSize: 28.sp,
                              //     color: Colors.black,
                              //   ),
                              // ),
                              Expanded(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      state.controller3.text,
                                      style: TextStyle(
                                        fontSize: 28.sp,
                                        color: Colors.black,
                                      ),
                                    ),
                                  ],
                                ),
                              ),

                              Icon(
                                Icons.arrow_forward_ios_rounded,
                                color: Color(0xffcccccc),
                                size: 14,
                              )
                            ],
                          ),
                        ),
                      ),
                      // 详细地址
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 20.w),
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 32.w),
                          decoration: BoxDecoration(
                            border: Border(
                                bottom: BorderSide(
                              color: Color(0xfff0f0f0),
                            )),
                          ),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              Container(
                                width: 180.w,
                                child: Text(
                                  '详细地址',
                                  style: TextStyle(
                                    color: Color(0xff666666),
                                    fontSize: 28.sp,
                                  ),
                                ),
                              ),
                              Container(
                                width: 520.w,
                                child: TextField(
                                  controller: state.controller4,
                                  enabled: true,
                                  textAlignVertical: TextAlignVertical.center,
                                  textAlign: TextAlign.start,
                                  maxLines: null,
                                  style: TextStyle(
                                    fontSize: 28.sp,
                                  ),
                                  decoration: InputDecoration(
                                    isDense: true,
                                    contentPadding: EdgeInsets.all(0),
                                    hintText: '请输入详细地址',
                                    hintStyle: TextStyle(
                                      fontSize: 28.sp,
                                      color: Color(0xffcccccc),
                                    ),
                                    border: InputBorder.none,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      TextInputWidget(
                        title: "标签",
                        suffixWidget: GetBuilder<AddressUpdateLogic>(
                          builder: (l) {
                            return Wrap(
                              children: [
                                GestureDetector(
                                  onTap: () {
                                    state.sex = 1;
                                    l.update();
                                  },
                                  child: Container(
                                    padding: EdgeInsets.symmetric(
                                      horizontal: 30.w,
                                      vertical: 2.w,
                                    ),
                                    margin: EdgeInsets.only(
                                        left: 20.w, bottom: 20.w),
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(40),
                                      border: Border.all(
                                        color: Color(0xffcccccc),
                                      ),
                                      color: state.sex == 1
                                          ? Config.mainColor
                                          : Color(0xffF5F5F5),
                                    ),
                                    child: Text(
                                      state.tagList[0],
                                      style: TextStyle(
                                        color: state.sex == 1
                                            ? Colors.white
                                            : Color(0xffcccccc),
                                      ),
                                    ),
                                  ),
                                ),
                                GestureDetector(
                                  onTap: () {
                                    state.sex = 2;
                                    l.update();
                                  },
                                  child: Container(
                                    padding: EdgeInsets.symmetric(
                                      horizontal: 30.w,
                                      vertical: 2.w,
                                    ),
                                    margin: EdgeInsets.only(
                                        left: 20.w, bottom: 20.w),
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(40),
                                      border: Border.all(
                                        color: Color(0xffcccccc),
                                      ),
                                      color: state.sex == 2
                                          ? Config.mainColor
                                          : Color(0xffF5F5F5),
                                    ),
                                    child: Text(
                                      state.tagList[1],
                                      style: TextStyle(
                                        color: state.sex == 2
                                            ? Colors.white
                                            : Color(0xffcccccc),
                                      ),
                                    ),
                                  ),
                                ),
                                if (state.tagList[2] != "")
                                  GestureDetector(
                                    onTap: () {
                                      state.sex = 3;
                                      l.update();
                                    },
                                    child: Container(
                                      padding: EdgeInsets.symmetric(
                                        horizontal: 30.w,
                                        vertical: 2.w,
                                      ),
                                      margin: EdgeInsets.only(
                                          left: 20.w, bottom: 20.w),
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(40),
                                        border: Border.all(
                                          color: Color(0xffcccccc),
                                        ),
                                        color: state.sex == 3
                                            ? Config.mainColor
                                            : Color(0xffF5F5F5),
                                      ),
                                      child: Text(
                                        state.tagList[2],
                                        style: TextStyle(
                                          color: state.sex == 3
                                              ? Colors.white
                                              : Color(0xffcccccc),
                                        ),
                                      ),
                                    ),
                                  ),
                                GestureDetector(
                                  onTap: () {
                                    showDialog(
                                      context: context,
                                      barrierDismissible: false,
                                      builder: (BuildContext context) {
                                        return AlertDialog(
                                          title: Text('添加地址标签'),
                                          content: TextField(
                                            controller: state.tagController,
                                            textAlignVertical:
                                                TextAlignVertical.center,
                                            onChanged: (value) {},
                                            decoration: InputDecoration(
                                              isDense: true,
                                              border: InputBorder.none,
                                              hintText: '请输入自定义标签',
                                              hintStyle: TextStyle(
                                                fontSize: 28.sp,
                                                color: Color(0xffcccccc),
                                              ),
                                            ),
                                          ),
                                          actions: <Widget>[
                                            FlatButton(
                                              onPressed: () =>
                                                  Navigator.of(context).pop(),
                                              child: Text('取消'),
                                            ),
                                            FlatButton(
                                              onPressed: () {
                                                if (state.tagController.text
                                                    .isNotEmpty) {
                                                  SpUtil.putString("addressTag",
                                                      state.tagController.text);

                                                  state.tagList[2] =
                                                      state.tagController.text;

                                                  l.update();
                                                }

                                                Navigator.of(context).pop();
                                              },
                                              textColor: Colors.red,
                                              child: Text('确定'),
                                            ),
                                          ],
                                        );
                                      },
                                    );
                                  },
                                  child: Container(
                                    padding: EdgeInsets.symmetric(
                                      horizontal: 30.w,
                                      vertical: 2.w,
                                    ),
                                    margin: EdgeInsets.only(left: 20.w),
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(40),
                                      border: Border.all(
                                        color: Color(0xffcccccc),
                                      ),
                                    ),
                                    child: Text(
                                      "自定义",
                                      style:
                                          TextStyle(color: Color(0xffcccccc)),
                                    ),
                                  ),
                                ),
                              ],
                            );
                          },
                        ),
                      ),
                      TextInputWidget(
                        title: "设为默认",
                        suffixWidget: GetBuilder<AddressUpdateLogic>(
                          builder: (logic) {
                            return Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                Switch(
                                  value: state.isSelect,
                                  inactiveThumbColor: Colors.white,
                                  inactiveTrackColor: Colors.grey,
                                  activeColor: Colors.white,
                                  activeTrackColor: Colors.deepOrange,
                                  onChanged: (isSelect) {
                                    state.isSelect = isSelect;
                                    logic.update();
                                  },
                                ),
                              ],
                            );
                          },
                        ),
                      ),
                    ],
                  ),
                  Column(
                    children: [
                      Container(
                          color: Colors.white,
                          padding: EdgeInsets.symmetric(
                            vertical: 30.w,
                            horizontal: 46.w,
                          ),
                          child: GestureDetector(
                            onTap: () {
                              logic.updateAdress();
                            },
                            child: Padding(
                              padding: EdgeInsets.symmetric(
                                horizontal: 20.w,
                              ),
                              child: ButtonWidget(title: "保存地址"),
                            ),
                          )),
                      Container(
                          color: Colors.white,
                          padding: EdgeInsets.symmetric(
                            vertical: 30.w,
                            horizontal: 46.w,
                          ),
                          margin: EdgeInsets.only(bottom: 10),
                          child: GestureDetector(
                            // onTap: logic.delAddress,
                            onTap: () {
                              showDialog(
                                context: context,
                                builder: (context) {
                                  return CupertinoAlertDialog(
                                    title: Text(
                                      '提示',
                                      style: TextStyle(
                                        color: Color(0xff232323),
                                        fontSize: 17,
                                      ),
                                    ),
                                    content: Column(
                                      children: [
                                        SizedBox(height: setWidth(10)),
                                        Text(
                                          '确定要删除吗？',
                                          style: TextStyle(
                                            color: Color(0xffaaaaaa),
                                            fontSize: 15,
                                          ),
                                        ),
                                      ],
                                    ),
                                    actions: [
                                      CupertinoDialogAction(
                                        child: Text("取消"),
                                        onPressed: () {
                                          print('取消点击事件！！！');
                                          Navigator.pop(context);
                                        },
                                      ),
                                      CupertinoDialogAction(
                                        child: Text("确定"),
                                        onPressed: () {
                                          logic.delAddress();
                                          Navigator.pop(context);
                                        },
                                      ),
                                    ],
                                  );
                                },
                              );
                            },
                            behavior: HitTestBehavior.opaque,
                            child: Padding(
                              padding: EdgeInsets.symmetric(
                                horizontal: 20.w,
                              ),
                              child: ButtonWidget(
                                  title: "删除地址",
                                  border: Border.all(color: Config.mainColor),
                                  bgColor: Colors.white,
                                  textColor: Config.mainColor),
                            ),
                          ))
                    ],
                  )
                ],
              )));
        }));
  }

  Result resultArr = new Result();
  void _clickEventFunc(context) async {
    // final res = await KzcityPicker.showPicker(
    //   context,
    //   datas: state.addressList,
    // );
    // if (res != null) { // 刷新
    //   logic.selectCity(res);
    // }

    XYZCityPicker.showPicker(
        province: state.province,
        city: state.city,
        district: state.district,
        street: state.street,
        provinceListBack: state.provinceList,
        cityListBack: state.cityList,
        cityAreaListBack: state.cityAreaList,
        townListBack: state.townList,
        context: context,
        onChange: (int index, Map checkedItem, List backAddressItemList) {
          logic.selectCity(index, checkedItem, backAddressItemList);
        });
  }
}
